package com.example.about_example.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("return")
//@RestController 返回数据
//@RestController = @Controller + @ResponseBody(返回请求正文)
@Controller //返回数据+视图
public class ReturnController {
    @RequestMapping("index")
    public String returnIndex(){
        return "/index.html";
    }
    @ResponseBody
    @RequestMapping("returnData")
    public String returnData(){
        return "返回数据";
    }
    @ResponseBody
    @RequestMapping("returnHtml")
    public String returnHtml(){
        return "<h1>又是美好的一天</h1>";
    }
    @ResponseBody
    @RequestMapping("returnJson")
    public Person returnJson(){
        Person person = new Person();
        person.setId(1);
        person.setName("lisi");
        person.setAge(18);
        return person;
    }
    @ResponseBody
    @RequestMapping("/returnMap")
    public Map<String,String> returnMap(){
        Map<String,String> kv = new HashMap<>();
        kv.put("zhangsan","18");
        kv.put("lisi","19");
        kv.put("zhaoliu","20");
        return kv;
    }
    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(401);//通常表示没有登录
        return "设置状态码";
    }

    @ResponseBody
    @RequestMapping(value = "/m1",produces = "application/json;charset=utf-8")
//    @RequestMapping(value = "/m1")
    public String m1(HttpServletResponse response){
        //设置header
        response.setHeader("bighead","bighead");
        return "{'cw':8}";
    }
}
